package web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import dto.MenuDto;
import entity.Menu;
import enums.travelenums;
import service.Menu_service;

@Controller
public class Menu_controller {
	
	@Autowired
	private Menu_service menu_service;
	
	@RequestMapping(value="menulist",method=RequestMethod.POST)
	@ResponseBody
	public List<MenuDto> MenuList(){
		return menu_service.MenuList();
	}
	
	//新增或修改主菜单
	@RequestMapping(value="menu/main",method=RequestMethod.POST)
	@ResponseBody
	public String MainMenu(Menu menu){
		if(menu!=null){
			Menu Sql_menu=menu_service.get_MenuById(menu.getId());
			if(Sql_menu!=null){
				Sql_menu.setMenuName(menu.getMenuName());
				boolean boo=menu_service.update_menu(Sql_menu);
				if(boo){
					return travelenums.SUCCESS.getInfo();
				}
				return travelenums.ERRO.getInfo();
			}else{
				menu.setParentId(0); //父菜单父id为0
				boolean boo=menu_service.add_menu(menu);
				if(boo){
					return travelenums.SUCCESS.getInfo();
				}
				return travelenums.ERRO.getInfo();
			}
		}
		return travelenums.ERRO.getInfo();
	}
	
	//新增或修改子菜单
		@RequestMapping(value="menu/child",method=RequestMethod.POST)
		@ResponseBody
		public String ChildMenu(Menu menu){
			if(menu!=null){
				Menu Sql_menu=menu_service.get_MenuById(menu.getId());
				if(Sql_menu!=null){
					Sql_menu.setMenuName(menu.getMenuName());
					Sql_menu.setUrl(menu.getUrl());
					boolean boo=menu_service.update_menu(Sql_menu);
					if(boo){
						return travelenums.SUCCESS.getInfo();
					}
					return travelenums.ERRO.getInfo();
				}else{
					boolean boo=menu_service.add_menu(menu);
					if(boo){
						return travelenums.SUCCESS.getInfo();
					}
					return travelenums.ERRO.getInfo();
				}
			}
			return travelenums.ERRO.getInfo();
		}
	
		//删除主菜单和其子菜单
		@RequestMapping(value="mainMenu/delete",method=RequestMethod.POST)
		@ResponseBody
		public String MainMenuDelete(Integer id,String[] ChildIds){
			if(menu_service.delete_menu(id)&&menu_service.batchDelete_menu(ChildIds)){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		
		//删除子菜单
		@RequestMapping(value="childMenu/delete",method=RequestMethod.POST)
		@ResponseBody
		public String ChildMenuDelete(Integer id){
			if(menu_service.delete_menu(id)){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}

}
